package com.huluobo.a2204kotlinshopping.search

import androidx.lifecycle.MutableLiveData
import com.huluobo.base.bean.HomeGoodsBeanResult
import com.huluobo.base.bean.KeyWordBean
import com.huluobo.base.model.BaseRepo
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers

/**
 *  Created by LC on 2024/7/26.
 */
class SearchRepo : BaseRepo() {
    fun getGoodsList(
        categoryId: Int,
        currentPage: Int,
        pageSize: Int,
        success: MutableLiveData<HomeGoodsBeanResult>,
        failed: MutableLiveData<String>
    ) {
        apiService.getHomeGoodsList(categoryId, currentPage, pageSize)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<HomeGoodsBeanResult> {
                override fun onSubscribe(d: Disposable) {
                }

                override fun onError(e: Throwable) {
                    failed.value = e.message
                }

                override fun onComplete() {
                }

                override fun onNext(t: HomeGoodsBeanResult) {
                    success.value = t
                }
            })
    }

    fun queryKeyword(success: MutableLiveData<List<KeyWordBean>>, failed: MutableLiveData<String>) {
        db.keywordsDao.queryAllKeywords()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<List<KeyWordBean>> {
                override fun onSubscribe(d: Disposable) {
                }

                override fun onError(e: Throwable) {
                    failed.value = e.message
                }

                override fun onComplete() {
                }

                override fun onNext(t: List<KeyWordBean>) {
                    success.value = t
                }
            })
    }

    fun deleteKeyword(keyWordBean: KeyWordBean) {
        db.keywordsDao.deleteKeyword(keyWordBean)
    }
}